import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;

public class FilledOvalTool extends Tool{

	  protected Point startingMousePosition;
	  protected Point currentMousePosition;
	  protected Color saveColor;

	  public FilledOvalTool(DrawingCanvas canvas, LinkedList<ShapeObject> ShapeObjects, MDUser mdUser) {
		  super(canvas, ShapeObjects, mdUser);
	  }

	  
	  protected void drawFilledOval(int x, int y, int width, int height) {
	    int ovalX = x;
	    int ovalY = y;
	    int ovalWidth = width;
	    int ovalHeight = height;
	    if (width < 0) {
	      ovalX = x + width;
	      ovalWidth = -width;
	    } 
	    if (height < 0) {
	      ovalY = y + height;
	      ovalHeight = -height;
	    }
	    Graphics iBGraphics = canvas.getImageBufferGraphics();
	    iBGraphics.fillOval(ovalX, ovalY, ovalWidth, ovalHeight);
	      
	    canvas.repaint();
	  }

	 public void mousePressed(MouseEvent e)  {
	   /* Establish starting point for next drawing */
	   startingMousePosition = e.getPoint();
	   currentMousePosition = startingMousePosition;
	   Graphics iBGraphics = canvas.getImageBufferGraphics();
	   saveColor = iBGraphics.getColor( );
	   iBGraphics.setXORMode(Color.lightGray);
	   iBGraphics.setColor(Color.white);
	   drawFilledOval(startingMousePosition.x,
			 startingMousePosition.y, 1,1);
							
	 }

	 public void mouseDragged(MouseEvent e)  {
	   Point newMousePosition = e.getPoint();
	   drawFilledOval(startingMousePosition.x, startingMousePosition.y,
	         	currentMousePosition.x -startingMousePosition.x +1,
			currentMousePosition.y - startingMousePosition.y +1);
	   drawFilledOval(startingMousePosition.x,startingMousePosition.y,
	                 newMousePosition.x -startingMousePosition.x + 1,
			 newMousePosition.y- startingMousePosition.y + 1);
	   /* update current mouse coordinates */
	   currentMousePosition = newMousePosition;
	 }

	  public void mouseReleased(MouseEvent e) { 
			Point newMousePosition = e.getPoint();
			drawFilledOval(startingMousePosition.x,startingMousePosition.y,
			                 newMousePosition.x -startingMousePosition.x + 1,
			                 newMousePosition.y- startingMousePosition.y + 1);
		    Graphics iBGraphics = canvas.getImageBufferGraphics();
			ShapeObjects.add(new FilledOvalObject(startingMousePosition, currentMousePosition, saveColor));	    
			iBGraphics.setPaintMode();
			paintObjects(0);
			iBGraphics.setColor(saveColor);
	  }

	  public void keyPressed(KeyEvent e) {}

}
